﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YArchitech.Revit.Electric.Common;

namespace HYCableTrayConnection
{
	internal class FittingConnectorRelevantParamer
	{
		public static Dictionary<Connector, CableTrayFittingSizeParamer> GetFittingRelevantParamer(VerticalCableTrayFittingType type, FamilyInstance fitting)
		{
			Dictionary<Connector, CableTrayFittingSizeParamer> dictionary = new Dictionary<Connector, CableTrayFittingSizeParamer>();
			List<Connector> conduitFittingConnectors = RevitUtils.GetConduitFittingConnectors(fitting);
			switch (type)
			{
			case VerticalCableTrayFittingType.LeftDownSideElbow:
			case VerticalCableTrayFittingType.RightDownSideElbow:
			case VerticalCableTrayFittingType.LeftUpSideElbow:
			case VerticalCableTrayFittingType.RightUpSideElbow:
				dictionary.Add(conduitFittingConnectors[0], CableTrayFittingSizeParamer.BothAB);
				break;
			case VerticalCableTrayFittingType.VerticalUpTee:
			case VerticalCableTrayFittingType.VerticalDownTee:
				FittingConnectorRelevantParamer.GetVerticalTeeAssociatedSize(conduitFittingConnectors, ref dictionary);
				break;
			case VerticalCableTrayFittingType.VerticalUpSideTee:
			case VerticalCableTrayFittingType.VerticalDownSideTee:
				FittingConnectorRelevantParamer.GetVerticalTeeAssociatedSize(conduitFittingConnectors, type, ref dictionary);
				break;
			case VerticalCableTrayFittingType.VerticalUpCornerTeeA:
			case VerticalCableTrayFittingType.VerticalDownCornerTeeA:
			case VerticalCableTrayFittingType.VerticalUpCornerTeeB:
			case VerticalCableTrayFittingType.VerticalDownCornerTeeB:
				FittingConnectorRelevantParamer.GetCornerVerticalTeeAssociatedSize(conduitFittingConnectors, type, ref dictionary);
				break;
			case VerticalCableTrayFittingType.VerticalLeftTee:
			case VerticalCableTrayFittingType.VerticalRightTee:
				FittingConnectorRelevantParamer.GetVerticalTeeAssociatedSize(conduitFittingConnectors, type, ref dictionary);
				break;
			case VerticalCableTrayFittingType.VerticalLeftCross:
			case VerticalCableTrayFittingType.VerticalRightCross:
				FittingConnectorRelevantParamer.GetDirectionVerticalCrossAssociatedSize(conduitFittingConnectors, ref dictionary);
				break;
			case VerticalCableTrayFittingType.VerticalUpCrossA:
			case VerticalCableTrayFittingType.VerticalUpCrossB:
			case VerticalCableTrayFittingType.VerticalUpCrossC:
			case VerticalCableTrayFittingType.VerticalDownCrossA:
			case VerticalCableTrayFittingType.VerticalDownCrossB:
			case VerticalCableTrayFittingType.VerticalDownCrossC:
				FittingConnectorRelevantParamer.GetUpDownVerticalCrossParam(conduitFittingConnectors, type, ref dictionary);
				break;
			case VerticalCableTrayFittingType.VerticalCrossA:
			case VerticalCableTrayFittingType.VerticalCrossB:
				FittingConnectorRelevantParamer.GetVerticalCrossAssociatedSize(conduitFittingConnectors, ref dictionary);
				break;
			}
			return dictionary;
		}

		private static void GetVerticalTeeAssociatedSize(List<Connector> conns, ref Dictionary<Connector, CableTrayFittingSizeParamer> dicAssociatedSize)
		{
			Connector key = null;
			Connector key2 = null;
			foreach (Connector connector in conns)
			{
				if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(XYZ.BasisZ, 0.01) || connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-XYZ.BasisZ, 0.01))
				{
					key = connector;
				}
				else
				{
					key2 = connector;
				}
			}
			dicAssociatedSize.Add(key2, CableTrayFittingSizeParamer.BothAB);
			dicAssociatedSize.Add(key, CableTrayFittingSizeParamer.B1);
		}

		private static void GetVerticalTeeAssociatedSize(List<Connector> conns, VerticalCableTrayFittingType type, ref Dictionary<Connector, CableTrayFittingSizeParamer> dicAssociatedSize)
		{
			Connector key = null;
			Connector key2 = null;
			foreach (Connector connector in conns)
			{
				if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(XYZ.BasisZ, 0.01) || connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-XYZ.BasisZ, 0.01))
				{
					key = connector;
				}
				else
				{
					key2 = connector;
				}
			}
			if (type == VerticalCableTrayFittingType.VerticalUpSideTee || type == VerticalCableTrayFittingType.VerticalDownSideTee)
			{
				dicAssociatedSize.Add(key2, CableTrayFittingSizeParamer.BothAB);
				dicAssociatedSize.Add(key, CableTrayFittingSizeParamer.BothA1B1);
				return;
			}
			if (type == VerticalCableTrayFittingType.VerticalLeftTee || type == VerticalCableTrayFittingType.VerticalRightTee)
			{
				dicAssociatedSize.Add(key, CableTrayFittingSizeParamer.BothAB);
				dicAssociatedSize.Add(key2, CableTrayFittingSizeParamer.BothA1B1);
			}
		}

		private static void GetCornerVerticalTeeAssociatedSize(List<Connector> conns, VerticalCableTrayFittingType type, ref Dictionary<Connector, CableTrayFittingSizeParamer> dicAssociatedSize)
		{
			Connector connector = null;
			Connector connector2 = null;
			Connector key = null;
			foreach (Connector connector3 in conns)
			{
				if (connector3.CoordinateSystem.BasisZ.IsAlmostEqualTo(XYZ.BasisZ, 0.01) || connector3.CoordinateSystem.BasisZ.IsAlmostEqualTo(-XYZ.BasisZ, 0.01))
				{
					connector = connector3;
					break;
				}
			}
			foreach (Connector connector4 in conns)
			{
				if (!connector4.Origin.IsAlmostEqualTo(connector.Origin, 0.01) && (connector4.CoordinateSystem.BasisZ.IsAlmostEqualTo(connector.CoordinateSystem.BasisY, 0.01) || connector4.CoordinateSystem.BasisZ.IsAlmostEqualTo(-connector.CoordinateSystem.BasisY, 0.01)))
				{
					connector2 = connector4;
					break;
				}
			}
			foreach (Connector connector5 in conns)
			{
				if (!connector5.Origin.IsAlmostEqualTo(connector.Origin, 0.01) && !connector5.Origin.IsAlmostEqualTo(connector2.Origin, 0.01) && connector5 != connector && connector5 != connector2)
				{
					key = connector5;
					break;
				}
			}
			if (type == VerticalCableTrayFittingType.VerticalDownCornerTeeA || type == VerticalCableTrayFittingType.VerticalDownCornerTeeB || type == VerticalCableTrayFittingType.VerticalUpCornerTeeA || type == VerticalCableTrayFittingType.VerticalUpCornerTeeB)
			{
				dicAssociatedSize.Add(connector2, CableTrayFittingSizeParamer.BothAB);
				dicAssociatedSize.Add(key, CableTrayFittingSizeParamer.A1);
				dicAssociatedSize.Add(connector, CableTrayFittingSizeParamer.B1);
			}
		}

		private static void GetDirectionVerticalCrossAssociatedSize(List<Connector> conns, ref Dictionary<Connector, CableTrayFittingSizeParamer> dicAssociatedSize)
		{
			Connector connector = null;
			Connector key = null;
			foreach (Connector connector2 in conns)
			{
				if (connector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(XYZ.BasisZ, 0.01) || connector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(-XYZ.BasisZ, 0.01))
				{
					connector = connector2;
					break;
				}
			}
			foreach (Connector connector3 in conns)
			{
				if (!connector3.Origin.IsAlmostEqualTo(connector.Origin, 0.01) && (connector3.CoordinateSystem.BasisZ.IsAlmostEqualTo(connector.CoordinateSystem.BasisX, 0.01) || connector3.CoordinateSystem.BasisZ.IsAlmostEqualTo(-connector.CoordinateSystem.BasisX, 0.01)))
				{
					key = connector3;
					break;
				}
			}
			dicAssociatedSize.Add(key, CableTrayFittingSizeParamer.BothAB);
			dicAssociatedSize.Add(connector, CableTrayFittingSizeParamer.BothA1B1);
		}

		private static void GetVerticalCrossAssociatedSize(List<Connector> conns, ref Dictionary<Connector, CableTrayFittingSizeParamer> dicAssociatedSize)
		{
			Connector connector = null;
			Connector key = null;
			foreach (Connector connector2 in conns)
			{
				if (connector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(XYZ.BasisZ, 0.01) || connector2.CoordinateSystem.BasisZ.IsAlmostEqualTo(-XYZ.BasisZ, 0.01))
				{
					connector = connector2;
					break;
				}
			}
			foreach (Connector connector3 in conns)
			{
				if (!connector3.Origin.IsAlmostEqualTo(connector.Origin, 0.01) && !connector3.CoordinateSystem.BasisZ.IsAlmostEqualTo(XYZ.BasisZ, 0.01) && !connector3.CoordinateSystem.BasisZ.IsAlmostEqualTo(-XYZ.BasisZ, 0.01))
				{
					key = connector3;
					break;
				}
			}
			dicAssociatedSize.Add(key, CableTrayFittingSizeParamer.BothAB);
			dicAssociatedSize.Add(connector, CableTrayFittingSizeParamer.BothA1B1);
		}

		private static void GetUpDownVerticalCrossParam(List<Connector> conns, VerticalCableTrayFittingType type, ref Dictionary<Connector, CableTrayFittingSizeParamer> dicAssociatedSize)
		{
			Connector connector = null;
			Connector key = null;
			Connector connector2 = null;
			foreach (Connector connector3 in conns)
			{
				if (connector3.CoordinateSystem.BasisZ.IsAlmostEqualTo(XYZ.BasisZ, 0.01) || connector3.CoordinateSystem.BasisZ.IsAlmostEqualTo(-XYZ.BasisZ, 0.01))
				{
					connector = connector3;
					break;
				}
			}
			foreach (Connector connector4 in conns)
			{
				if (!connector4.Origin.IsAlmostEqualTo(connector.Origin, 0.01) && (connector4.CoordinateSystem.BasisZ.IsAlmostEqualTo(connector.CoordinateSystem.BasisX, 0.01) || connector4.CoordinateSystem.BasisZ.IsAlmostEqualTo(-connector.CoordinateSystem.BasisX, 0.01)))
				{
					connector2 = connector4;
					break;
				}
			}
			foreach (Connector connector5 in conns)
			{
				if (!connector5.Origin.IsAlmostEqualTo(connector.Origin, 0.01) && !connector5.Origin.IsAlmostEqualTo(connector2.Origin, 0.01) && (connector5.CoordinateSystem.BasisX.IsAlmostEqualTo(connector.CoordinateSystem.BasisX, 0.01) || connector5.CoordinateSystem.BasisX.IsAlmostEqualTo(-connector.CoordinateSystem.BasisX, 0.01)))
				{
					key = connector5;
					break;
				}
			}
			if (VerticalCableTrayFittingType.VerticalUpCrossA == type || VerticalCableTrayFittingType.VerticalDownCrossA == type)
			{
				dicAssociatedSize.Add(connector2, CableTrayFittingSizeParamer.BothAB);
				dicAssociatedSize.Add(connector, CableTrayFittingSizeParamer.BothA1B1);
				return;
			}
			if (VerticalCableTrayFittingType.VerticalUpCrossB == type || VerticalCableTrayFittingType.VerticalDownCrossB == type || VerticalCableTrayFittingType.VerticalUpCrossC == type || VerticalCableTrayFittingType.VerticalDownCrossC == type)
			{
				dicAssociatedSize.Add(key, CableTrayFittingSizeParamer.BothAB);
				dicAssociatedSize.Add(connector2, CableTrayFittingSizeParamer.A1);
				dicAssociatedSize.Add(connector, CableTrayFittingSizeParamer.B1);
			}
		}
	}
}
